package com.example.booksmanagementsystem.controller;

import com.example.booksmanagementsystem.entity.Book;
import com.example.booksmanagementsystem.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@Controller
public class BookController {

    @Autowired
    BookService bookService;

    //日志
    java.util.logging.Logger logger = Logger.getLogger(this.getClass().getName());


    //获取所有图书功能
    @RequestMapping("/getAllBooks")
    public @ResponseBody List<Book> getAllBooks() {
        return bookService.getAllBooks();
    }

    //分页获取所有图书
    @RequestMapping(value = "/getBooksByPage", method = RequestMethod.POST)
    public @ResponseBody Map<String, Object> getBooksByPage(@RequestBody Map<String, String> map) {
        //第page页，每页size条
        int page = Integer.valueOf(map.get("page"));
        int size = Integer.valueOf(map.get("size"));
        if (page <= 0) page = 1;
        Map<String, Object> resultMap = new HashMap<>();
        List<Book> bookList = bookService.getBooksByPage((page - 1) * size, size);
        resultMap.put("bookList", bookList);
        resultMap.put("nums", bookService.getBookCount());
        resultMap.put("page", page);
        resultMap.put("size", size);

        return resultMap;
    }

    //按名字模糊获取图书
    @RequestMapping("/getBooksByName")
    public @ResponseBody List<Book> getBooksByName(@RequestBody Map<String, String> map) {
        String name = map.get("name");
        return bookService.searchBooksByName(name);
    }

    //删除图书功能
    @RequestMapping("/deleteBook")
    public @ResponseBody String deleteBook(@RequestBody Map<String, String> map) {
        int id = Integer.parseInt(map.get("id"));
        bookService.deleteBook(id);
        return "ok";
    }

    //增加图书功能
    @RequestMapping("/addBook")
    public @ResponseBody String addBook(@RequestBody Map<String, String> map) throws Exception {

        //获取参数
        String name = map.get("name");
        String type = map.get("type");
        String author = map.get("author");
        Float price = Float.valueOf(map.get("price"));
        String pic = map.get("pic");

        //调试
        logger.info("开始添加图书");
        logger.info("name:" + name);

        //日期格式化
        String createDate = String.valueOf(map.get("createDate"));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date parsedDate = dateFormat.parse(createDate);

        //保存到数据库
        bookService.addBook(new Book(-1, name, type, author, price, pic, parsedDate));
        return "ok";
    }

    //修改图书
    @RequestMapping(value = "/updateBook", method = RequestMethod.POST)
    @ResponseBody
    public String updateBook(@RequestBody Map<String, String> maps) throws Exception {
        //编号
        String id = maps.get("id");
        //名字
        String name = maps.get("name");
        //类型
        String type = maps.get("type");
        //作者
        String author = maps.get("author");
        //价格
        float price = Float.parseFloat(maps.get("price"));
        //图片
        String pic = maps.get("pic");

        //出版日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date createDate = formatter.parse(maps.get("createDate"));

        //查询
        Book book = bookService.getBookById(Integer.parseInt(id));
        logger.info("book.id=" + book.id);

        if (book == null) {
            return "修改的书不存在！";
        }

        //修改数据
        book.name = name;
        book.type = type;
        book.author = author;
        book.pic = pic;
        book.price = price;
        book.createDate = createDate;

        //提交修改
        logger.info("开始修改！");
        bookService.updateBook(book);

        return "ok";
    }
}
